<?php
require_once "../class/fenajax.class.php";
include "../dyn/connexion.php";

$f = new fenAjax("Formulaire inscription atelier",0);

if(isset($_POST['mode']) and isset($_POST['id_atelier'])) {
	
	$mode = $_POST['mode'];
	$id_atelier = $_POST['id_atelier'];
	
	$id = $_SESSION['user_id'];
	
	if( !in_array($mode,array(1,2)))
		$f->ajaxErreur("Fonction non supportée","Ce mode n'est pas supporté pour ce module");
		
	// mode 1 : inscription user
	// mode 2 : désinscription user
		
	if($mode == 1){		$header = "M'inscrire à l'atelier"; 		$nomAction = "M'inscrire";		$disable = ""; }
	if($mode == 2){		$header = "Me désinscrire de l'atelier"; 	$nomAction = "Me désinscrire";	$disable = ""; }
		
	$f->retourAjax["boutton"] = $nomAction;
	$f->retourAjax["header"] = $header;
	
	$form = "<div id=\"feedback\"></div><br />";
	
	if ($mode == 1)	{
		
		$form .= "<form action=\"#\" method=\"post\" onSubmit=\"return actionInscAtelier($(this));\">";
		
		$res = mysql_query("SELECT * FROM Atelier WHERE id = ".$id_atelier.";");
		
		while($row = mysql_fetch_array($res)) {
			
			$form .= "<div class=\"center\">";
			$form .= BMInfo("Note: Confirmer votre inscription à l'atelier : ".$row['nom']."",false);
			$form .= "</div>";
			
		}
		
		$form .= "<input type=\"hidden\" name=\"id_atelier\" value=\"".$id_atelier."\" />";
	}
	
	if ($mode == 2) {
	
		$form .= "<form action=\"#\" method=\"post\" onSubmit=\"return actionDesinscAtelier($(this));\">";
		
		$res = mysql_query("SELECT * FROM PartiUserAtelier WHERE idUser = ".$id.";");
		
		
		if (mysql_num_rows($res) == 0) {
			
			$form .= BMInfo("Vous n'êtes inscrit à aucun atelier");
			
			$form .= "<input type=\"hidden\" name=\"id_atelier\" value=\"".$id_atelier."\" />";
		}
		else {

			$form .= "<br /><label class=\"formInfos\" for=\"id_atelier\">Atelier :</label>
							<select id=\"id_atelier\" name=\"id_atelier\" $disable>";
			
			$prems = true;
			
			while($row = mysql_fetch_array($res)) {
				
				$res2 = mysql_query("SELECT * FROM Atelier WHERE id = ".$row['idAtelier'].";");
							
				while($row2 = mysql_fetch_array($res2)) {
					
					if ($prems) {		
						$form .= "<option value=\"".$row2['id']."\" selected=\"selected\">".$row2['nom']."</option>";
						$prems = false;
					}
					else {
						$form .= "<option value=\"".$row2['id']."\">".$row2['nom']."</option>";
					}
				}
			}
		
			$form .= "</select>";
		
		}
	}
	
	
	$form .= "<input type=\"hidden\" name=\"mode\" value=\"".$mode."\" />";
	
	$f->retourAjax["erreur"] = false;
	$f->retourAjax["msg"] = $form;
}

$f->endAjax();
?>